package com.iqik.base.zk.api;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class CreateNode implements Watcher {
    public static CountDownLatch countDownLatch = new CountDownLatch(1);
    public static ZooKeeper zooKeeper;

    /**
     * 回调方法
     * @param watchedEvent
     */
    @Override
    public void process(WatchedEvent watchedEvent) {
        // TODO
        if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
            // 解除主程序阻塞
//            countDownLatch.countDown();
            System.out.println("process方法执行了...");
            System.out.println("建立连接......");
            try {
                String nodePersistent = zooKeeper.create("/node-persistent", "持久节点 Content".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                System.out.println("持久节点：" + nodePersistent);
                String nodeEphemeral = zooKeeper.create("/node-ephemeral", "临时节点 Content".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                System.out.println("临时节点：" + nodeEphemeral);

                String nodePersistentSequential = zooKeeper.create("/node-persistent-seq", "持久节点 Content".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                System.out.println("持久节点：" + nodePersistentSequential);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建节点
     */
    private static void createNodeSync() throws KeeperException, InterruptedException {
        /**
         * path：节点创建的路径
         * data[]:数据byte类型
         * acl: 节点创建的权限信息：
         *      1。ANYONE_ID_UNSAFE: 表示任何人
         *      2。AUTH_IDS：此ID可用于设置ACL，它将被客户机验证的ID替换
         *      3。OPEN——ACL_UNSAFE: 完全开放的ACL（常用）-->word:anyone
         *      4。CREATOR_ALL_ACL：此ACL授予创建者身份验证ID的所有权限
         * createMode：创建节点的类型，
         *      1。PERSISTENT：持久节点
         *      2。PERSISTENT_SEQUENTIAL：持久顺序节点
         *      3。EPHEMERAL：临时节点
         *      4。EPHEMERAL_SEQUENTIAL：临时顺序节点
         * String node = zookeeper.create(path, data, acl, createMode);
         */
        System.out.println("创建节点。。。。。。。。。。。。");
        String nodePersistent = zooKeeper.create("/node-persistent", "持久节点 Content".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("持久节点：" + nodePersistent);
        String nodeEphemeral = zooKeeper.create("/node-ephemeral", "临时节点 Content".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("临时节点：" + nodeEphemeral);

        String nodePersistentSequential = zooKeeper.create("/node-persistent-seq", "持久节点 Content".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        System.out.println("持久节点：" + nodePersistentSequential);
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        /**
         * 客户端创建一个zk实例来连接服务
         * new ZooKeeper(connectString, sessionTimeOut, Watcher)
         * 参数依次：
         *  1。IP:PORT
         *  2.5000ms
         *  3.监听器（当特定事件除法监听时，zk会通过watcher通知客户端）
         */
        zooKeeper = new ZooKeeper("132.232.16.135:2181", 5000, new CreateNode());
        // 连接时是个异步的
        System.out.println(zooKeeper.getState());
        // 计数工具类，让main处于等待状态
//        countDownLatch.await();
        Thread.sleep(Integer.MAX_VALUE);
    }
}
